<html>
<head><meta charset="utf-8"><title>Unit tests for unordered serialization · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html">Unit tests for unordered serialization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241771164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20tests%20for%20unordered%20serialization/near/241771164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moritz Heiber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html#241771164">(Jun 07 2021 at 12:51)</a>:</h4>
<p>Hello everyone! o/<br>
I feel a little dumb for asking this here, but I'm at the end of my stick .. I have a function that produces serializable struct, and I want to make sure, given a certain input, that the struct is serialized correctly. However, when checking the output of the serialization (using <code>serde_json</code>) I'm running into flakiness because a part of the struct contains a data structure that's unordered (a <code>HashMap&lt;&amp;'static str, serde_json::Value&gt;</code>), therefore the serialization output can be slightly different, depending on where exactly certain values sit in the tree .. I'm struggling to come up with an idiomatic way of testing this, safe for simple "grepping" the string for content .. any pointers?</p>



<a name="241778667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20tests%20for%20unordered%20serialization/near/241778667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html#241778667">(Jun 07 2021 at 13:48)</a>:</h4>
<p>test round-tripping the struct? serialize, deserialize, compare</p>



<a name="241780474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20tests%20for%20unordered%20serialization/near/241780474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moritz Heiber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html#241780474">(Jun 07 2021 at 14:00)</a>:</h4>
<p>Yeah, that's what I ended up doing .. it just feels a little clunky</p>



<a name="241802250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20tests%20for%20unordered%20serialization/near/241802250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html#241802250">(Jun 07 2021 at 16:22)</a>:</h4>
<p>Other options would be testing with hashmaps containing only a single entry (nothing to reorder) or making your struct generic over the hashmap's hasher so you can use deterministic hash maps in your tests. or switch to an ordered map.<br>
But checking that things are round-trippable can be useful in itself, so it doesn't seem all that clunky.</p>



<a name="241808073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unit%20tests%20for%20unordered%20serialization/near/241808073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Moritz Heiber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization.html#241808073">(Jun 07 2021 at 17:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="330154">The 8472</span> <a href="#narrow/stream/122651-general/topic/Unit.20tests.20for.20unordered.20serialization/near/241802250">said</a>:</p>
<blockquote>
<p>Other options would be testing with hashmaps containing only a single entry (nothing to reorder) or making your struct generic over the hashmap's hasher so you can use deterministic hash maps in your tests. or switch to an ordered map.<br>
But checking that things are round-trippable can be useful in itself, so it doesn't seem all that clunky.</p>
</blockquote>
<p>Cheers, that makes sense .. I just might keep my implementation then (because changing the underlying implementation might lead to more issues)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>